<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Socks\Index\Ctl;

use Srv\Apps\Proto\VO\ResponseExtVO;
use Srv\Apps\Proto\VO\ResponseVO;
use Srv\Apps\Proto\VO\Socks\MsgBinaryVO;
use Srv\Apps\Proto\VO\Socks\MsgTextVO;
use Srv\Apps\Proto\VO\Socks\SubscribeListVO;
use Srv\Apps\Tasks\SocksSendTask;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\WebSocketTable;

final class IndexSocksCtl extends ControllerAbstract
{
    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        return self::tagGet();
    }

    /**
     * @param int $socketFd
     * @param SubscribeListVO $SubscribeListVO
     * @return bool
     * subscribeList
     */
    public function subscribeList(int $socketFd, SubscribeListVO $SubscribeListVO):bool
    {
        if($socketFd < 1) return false;
        $subKeyList         = [];
        if(WebSocketTable::getSkTableData($socketFd, $subKeyList)){
            if(count($subKeyList) > 0) $SubscribeListVO->setSubKeyList($subKeyList);
            return true;
        }
        return false;
    }

    /**
     * @param int $socketFd
     * @param array $subKeyList
     * @return bool
     * subscribeSet
     */
    public function subscribeSet(int $socketFd, array $subKeyList):bool
    {
        if($socketFd < 1) return false;
        $subKeyFailList     = [];
        if(!WebSocketTable::setSkTableData($socketFd, $subKeyList, $subKeyFailList)) ActionAbstract::addRunLogData($subKeyFailList);
        return true;
    }

    /**
     * @param int $socketFd
     * @param array $subKeyList
     * @return bool
     * subscribeAdd
     */
    public function subscribeAdd(int $socketFd, array $subKeyList):bool
    {
        if($socketFd < 1) return false;
        if(count($subKeyList) < 1) return true;
        $subKeyFailList     = [];
        if(!WebSocketTable::addSkTableData($socketFd, $subKeyList, $subKeyFailList)) ActionAbstract::addRunLogData($subKeyFailList);
        return true;
    }

    /**
     * @param int $socketFd
     * @param array $subKeyList
     * @return bool
     * subscribeDel
     */
    public function subscribeDel(int $socketFd, array $subKeyList):bool
    {
        if($socketFd < 1) return false;
        if(count($subKeyList) < 1) return true;
        $subKeyFailList     = [];
        if(!WebSocketTable::delSkTableData($socketFd, $subKeyList, $subKeyFailList)) ActionAbstract::addRunLogData($subKeyFailList);
        return true;
    }

    /**
     * @param int $socketFdFrom
     * @param array $subKeyList
     * @param string $text
     * @return bool
     * subscribeSendText
     */
    public function subscribeSendText(int $socketFdFrom, array $subKeyList, string $text):bool
    {
        if($socketFdFrom < 1) return false;
        if(count($subKeyList) > 0 && strlen($text) > 0){
            $MsgTextVO      = new MsgTextVO();
            $MsgTextVO->setText($text);
            $ResponseExtVO  = new ResponseExtVO();
            $ackNum         = WebSocketTable::genAckNum();
            $ResponseExtVO->setWsAckNum($ackNum);
            $ResponseVO     = new ResponseVO();
            $ResponseVO->setResultExt($ResponseExtVO);
            $ResponseVO->setResult($MsgTextVO->toAnyMessage());
            SocksSendTask::create()->sendTextBySubKey($subKeyList, $ResponseVO->getDataJsonStr([]), [], 0, 1, 2, $ackNum)->task();
        }
        return true;
    }

    /**
     * @param int $socketFdFrom
     * @param array $subKeyList
     * @param string $binary
     * @return bool
     * subscribeSendBinary
     */
    public function subscribeSendBinary(int $socketFdFrom, array $subKeyList, string $binary):bool
    {
        if($socketFdFrom < 1) return false;
        if(count($subKeyList) > 0 && strlen($binary) > 0){
            $MsgBinaryVO    = new MsgBinaryVO();
            $MsgBinaryVO->setBinary($binary);
            $ResponseExtVO  = new ResponseExtVO();
            $ackNum         = WebSocketTable::genAckNum();
            $ResponseExtVO->setWsAckNum($ackNum);
            $ResponseVO     = new ResponseVO();
            $ResponseVO->setResultExt($ResponseExtVO);
            $ResponseVO->setResult($MsgBinaryVO->toAnyMessage());
            SocksSendTask::create()->sendBinaryBySubKey($subKeyList, $ResponseVO->getDataJsonStr([]), [], 0, 1, 2, $ackNum)->task();
        }
        return true;
    }
}